package com.example.demo.practice.innerclass;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.demo.practice.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 普通的 ExpandableListView 的适配器
 */
public class BtnExpandableListAdapter extends BaseExpandableListAdapter {
    private static final String TAG = "BtnExpandableListAda";
    private String[] groupData;
    private List<List<String>> childData;


    public BtnExpandableListAdapter(String[] groupData, List<List<String>> childData) {
        this.groupData = groupData;
        this.childData = childData;
    }

    @Override
    public int getGroupCount() {
        return groupData.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        //return childData.get(groupPosition).size();
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupData[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childData.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View
            convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand_group_normal, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.label_group_normal);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.tvTitle.setText(groupData[groupPosition]);
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View
            convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_btn_child, parent, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.label_expand_child);
            childViewHolder.tvPhone = (TextView) convertView.findViewById(R.id.label_expand_phone);
            childViewHolder.adress = (TextView) convertView.findViewById(R.id.label_expand_address);
            childViewHolder.btnAccess = convertView.findViewById(R.id.btnAccess);
            childViewHolder.btnRefuse = convertView.findViewById(R.id.btnRefuse);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        childViewHolder.tvTitle.setText(childData.get(groupPosition).get(0));
        childViewHolder.tvPhone.setText(childData.get(groupPosition).get(1));
        childViewHolder.adress.setText(childData.get(groupPosition).get(2));
        class OnClick implements View.OnClickListener{

            @Override
            public void onClick(View view) {
                childData.remove(groupPosition);
                List<String> list= new ArrayList<>(Arrays.asList(groupData));
                list.remove(groupPosition);
                groupData= (String[]) list.toArray(new String[list.size()]);
                notifyDataSetChanged();//更新
            }
        }
        childViewHolder.btnAccess.setOnClickListener(new OnClick());
        childViewHolder.adress.setOnClickListener(new OnClick());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        Log.d(TAG, "onGroupExpanded() called with: groupPosition = [" + groupPosition + "]");
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        Log.d(TAG, "onGroupCollapsed() called with: groupPosition = [" + groupPosition + "]");
    }

    private static class GroupViewHolder {
        TextView tvTitle;
    }

    private static class ChildViewHolder {
        TextView tvTitle;
        TextView tvPhone;
        TextView adress;
        Button btnAccess;
        Button btnRefuse;
    }
}
